package com.gemantic.auth.controller;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import com.gemantic.auth.service.AuthSessionService;

/**
*
*
* created by wangshoufa
* 2018年7月4日 下午6:36:28
*
*/
@RestController
@CrossOrigin
public class SsoServerController {
	
Logger logger = LoggerFactory.getLogger(SsoServerController.class);
	
	@Autowired
	private AuthSessionService authSessionService;
	
	//校验token并注册地址
	@GetMapping(value="/verifyToken")
	public String verifyToken(String token, String address) {
		boolean resu = authSessionService.checkAndAddAddress(token, address);
		if (resu) {
			return "SUCCESS";
		}
		return "ERROR";
	}
	
	
	@GetMapping(value="/logoutByUser")
	public String logoutByUser(String userName) {
		String ssoToken = authSessionService.getUserToken(userName);
		if(ssoToken!=null) {
			List<String> addressList = authSessionService.logoutByUser(userName);
			if(addressList!=null) {
				addressList.stream().forEach(s -> authSessionService.sendLogout2Client(s,ssoToken));
			}
		}
		return "Done";
	}
	
	@GetMapping(value="/logoutByToken")
	public String logoutByToken(String ssoToken) {
		List<String> addressList = authSessionService.logoutByToken(ssoToken);
		if(addressList!=null) {
			addressList.stream().forEach(s -> authSessionService.sendLogout2Client(s,ssoToken));
		}
		return "logout";
	}

}
